package mapred.example.model;

import java.util.ArrayList;
import java.util.List;

import util.FieldUtil;

public class Title {

    private List<String> terms;

    public Title(String title) {
        this.terms = new ArrayList<String>();
        for (String t : title.split("\t")) {
            if (!t.isEmpty()) {
                terms.add(t);
            }
        }
    }

    public String getStr() {
        StringBuilder sb = new StringBuilder();
        for (String t : terms) {
            sb.append("\t");
            sb.append(t);
        }
        if (sb.length() == 0) {
            return "";
        } else {
            return sb.substring(1);
        }
    }

    public List<String> getTerms() {
        return terms;
    }

    public String toRLV(String[] query) {
        StringBuilder sb = new StringBuilder();
        sb.append(terms.size());
        for (String q : query) {
            List<Integer> poss = FieldUtil.getPositions(terms, q);
            sb.append("\1");
            sb.append(q);
            sb.append("\t");
            sb.append(poss.size());
            for (int pos : poss) {
                sb.append("\t");
                sb.append(pos);
                sb.append(" ");
                sb.append("0");
            }
        }
        return sb.toString();
    }

}
